package com.example.sfm.controller.admin;

import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.StudentRecordsDto;
import com.example.sfm.pojo.dto.StudentRecordsQueryDto;
import com.example.sfm.result.Result;
import com.example.sfm.service.IStudentRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@Tag(name = "管理员学生档案管理", description = "管理员对学生档案的管理接口")
@RestController("adminStudentRecordsController")
@RequestMapping("/admin/student-records")
@RequiredArgsConstructor
@Slf4j
public class StudentRecordsController {
    private final IStudentRecordsService recordsService;

    /**
     * 创建学生档案
     */
    @Operation(summary = "创建学生档案", description = "根据提供的学生档案信息创建一个新的学生档案")
    @PostMapping("/create")
    public Result<?> createRecord(@RequestBody @Validated @Parameter(description = "学生档案信息") StudentRecordsDto dto) {
        log.info("创建学生档案: {}", dto);
        recordsService.createRecord(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 分页查询档案
     */
    @Operation(summary = "分页查询学生档案", description = "根据查询条件分页查询学生档案列表")
    @GetMapping("/list")
    public Result<?> listRecords(@Validated @ParameterObject StudentRecordsQueryDto dto) {
        log.info("查询学生档案: {}", dto);
        return Result.success(recordsService.listRecords(dto), MessageConstants.SELECT_OK);
    }

    /**
     * 更新档案信息
     */
    @Operation(summary = "更新学生档案信息", description = "根据学生档案ID更新学生档案信息")
    @PutMapping("/update/{id}")
    public Result<?> updateRecord(@PathVariable @Parameter(description = "学生档案ID",in = ParameterIn.PATH) Integer id, @RequestBody @Validated @Parameter(description = "更新内容") StudentRecordsDto dto) {
        log.info("更新档案: ID={}, 内容={}", id, dto);
        recordsService.updateRecord(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除档案
     */
    @Operation(summary = "批量删除学生档案", description = "根据提供的学生档案ID列表批量删除学生档案")
    @DeleteMapping("/delete")
    public Result<?> deleteRecords(@RequestParam @Parameter(description = "学生档案ID列表",in = ParameterIn.QUERY) List<Integer> ids) {
        log.info("批量删除档案: IDs={}", ids);
        recordsService.deleteRecords(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * Excel导入学生档案
     */
    @Operation(summary = "Excel导入学生档案", description = "导入Excel文件中的学生档案数据")
    @PostMapping("/import")
    public Result<?> importExcel(@RequestParam("file") @Parameter(description = "上传的Excel文件") MultipartFile file) {
        log.info("导入学生档案: {}", file.getOriginalFilename());
        recordsService.importFromExcel(file);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * Excel导出学生档案
     */
    @Operation(summary = "Excel导出学生档案", description = "导出学生档案数据到Excel文件")
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response, @ParameterObject StudentRecordsQueryDto dto) {
        log.info("导出学生档案: {}", dto);
        recordsService.exportExcel(response, dto);
    }

    /**
     * 下载导入模板
     */
    @Operation(summary = "下载学生档案导入模板", description = "下载学生档案导入模板文件")
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        recordsService.downloadTemplate(response);
    }

    /**
     * 变更学籍状态
     */
    @Operation(summary = "变更学籍状态", description = "根据学生档案ID变更学籍状态")
    @PutMapping("/change-status/{id}")
    public Result<?> changeStatus(@PathVariable @Parameter(description = "学生档案ID",in = ParameterIn.PATH) Integer id,
                                  @RequestParam @Range(min = 0, max = 3) @Parameter(description = "新状态") Integer status) {
        log.info("变更学籍状态: ID={}, 新状态={}", id, status);
        recordsService.changeStatus(id, status);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}